package com.wsoft.dc.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.dc.entity.DcConfigEntity;
import com.wsoft.dc.mapper.DcConfigMapper;
import com.wsoft.dc.query.config.ConfigListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/10/23 09:08
 */
@Service
public class DcConfigManager extends ServiceImpl<DcConfigMapper, DcConfigEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Config.CONFIG_CODE,
            expire_sec = RedisKeyConstant.Config.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public DcConfigEntity getByCode(String code){
        if(StrUtil.isBlank(code)){
            return null;
        }
        QueryWrapper<DcConfigEntity> query = new QueryWrapper<>();
        query.lambda().eq(DcConfigEntity::getCode,code);
        return getOne(query);
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Config.CONFIG_ID,
            expire_sec = RedisKeyConstant.Config.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public DcConfigEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        DcConfigEntity entity = getById(id);
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Config.CONFIG_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Config.CONFIG_ID + id);
        }
        return b;
    }


    @Override
    public boolean updateById(DcConfigEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Config.CONFIG_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Config.CONFIG_ID + entity.getId());
        }
        return b;
    }

    /**
     * 根据模块id和类型查询
     * @param moduleId
     * @param type
     * @return
     */
    public DcConfigEntity getByModuleType(Long moduleId,String type){
        QueryWrapper<DcConfigEntity> query = new QueryWrapper<>();
        query.lambda().eq(DcConfigEntity::getModuleId,moduleId)
                .eq(DcConfigEntity::getType,type);
        return getOne(query);
    }

    /**
     * 列表查询
     * @param query
     * @return
     */
    public List<DcConfigEntity> listConfig(ConfigListQuery param) {
        QueryWrapper<DcConfigEntity> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotBlank(param.getCode()),DcConfigEntity::getCode,param.getCode())
                .like(StrUtil.isNotBlank(param.getName()),DcConfigEntity::getName,param.getName())
                .in(CollUtil.isNotEmpty(param.getType()),DcConfigEntity::getType,param.getType())
                .in(CollUtil.isNotEmpty(param.getModuleId()),DcConfigEntity::getModuleId,param.getModuleId())
                .eq(null != param.getIsEnable(),DcConfigEntity::getIsEnable,param.getIsEnable())
                .eq(null != param.getIsPendingProcess(),DcConfigEntity::getIsPendingProcess,param.getIsPendingProcess())
                .orderByAsc(DcConfigEntity::getSort);
        return list(query);
    }

    public List<DcConfigEntity> listConfigSortModuleSort(ConfigListQuery param) {
        QueryWrapper<DcConfigEntity> query = new QueryWrapper<>();
        query.lambda()
                .in(CollUtil.isNotEmpty(param.getType()),DcConfigEntity::getType,param.getType())
                .in(CollUtil.isNotEmpty(param.getModuleId()),DcConfigEntity::getModuleId,param.getModuleId())
                .orderByAsc(DcConfigEntity::getSort);
        return list(query);
    }

}
